﻿using System;
using Liber;
using Liber.SOA;
using Liber.ORM;
using Liber.BizTools;

namespace SoBusiness.App
{
    public class CompanyApp:SOAUnit
    {
        public CompanyApp(SOAHost _host):base(_host)
        {
            
        }

        [API("FindCompany")]
        FnResult<DyObject> FindCompany(SOARequestContext rq)
        {
            string k = rq.RequestContext.GetOrDefault<string>("key", "");
            var cmpnys = LiberBizHelper.OrgSVC.FindCompanies(k);
            return FnResult<DyObject>.Sucess(DyObject.FromObjList("companies", cmpnys));
        }

        [API("NewCompany")]
        FnResult<DyObject> NewCompany(SOARequestContext rq)
        {
            long uid = rq.RequestContext.GetOrDefault<long>("uid", -1);
            string name = rq.RequestContext.GetOrDefault<string>("name", "");
            string address = rq.RequestContext.GetOrDefault<string>("address", "");
            string phone = rq.RequestContext.GetOrDefault<string>("phone", "");
            string industry = rq.RequestContext.GetOrDefault<string>("industry", "");
            string website = rq.RequestContext.GetOrDefault<string>("website", "");
            string orgcode = rq.RequestContext.GetOrDefault<string>("orgcode", "");
            string licencecode = rq.RequestContext.GetOrDefault<string>("licencecode", "");
            string licencepic = rq.RequestContext.GetOrDefault<string>("licencepic", "");
            string logourl = rq.RequestContext.GetOrDefault<string>("logourl", "");
            long parentorg = rq.RequestContext.GetOrDefault<long>("parentorg", -1);

            var nc = LiberBizHelper.OrgSVC.CreateCompany(uid,-1,name, address, phone, industry, website, orgcode, licencecode, licencepic,logourl,false,-1,parentorg);

            return nc;
        }

        /// <summary>
        /// 重新发起企业认证申请
        /// </summary>
        /// <param name="rq"></param>
        /// <returns></returns>
        [API("ReapplyCompany")]
        FnResult<DyObject> ReapplyCompany(SOARequestContext rq)
        {
            long uid = rq.RequestContext.GetOrDefault<long>("uid", -1);
            long companyid = rq.RequestContext.GetOrDefault<long>("companyid", -1);
            string name = rq.RequestContext.GetOrDefault<string>("name", "");
            string address = rq.RequestContext.GetOrDefault<string>("address", "");
            string phone = rq.RequestContext.GetOrDefault<string>("phone", "");
            string industry = rq.RequestContext.GetOrDefault<string>("industry", "");
            string website = rq.RequestContext.GetOrDefault<string>("website", "");
            string orgcode = rq.RequestContext.GetOrDefault<string>("orgcode", "");
            string licencecode = rq.RequestContext.GetOrDefault<string>("licencecode", "");
            string licencepic = rq.RequestContext.GetOrDefault<string>("licencepic", "");
            string logourl = rq.RequestContext.GetOrDefault<string>("logourl", "");
            if (companyid == -1)
            {
                return FnResult<DyObject>.Failure(Name, "RequiredArgs").SetError("companyid", "empty or invalide");
            }

            var nc= LiberBizHelper.OrgSVC.CreateCompany(uid, -1, name, address, phone, industry, website, orgcode, licencecode, licencepic, logourl, true);
            return nc;
        }
    }
}
